#include <QtTest>

#include <QDebug>
#include <QMetaType>

#include "Az.h"
#include "TestFields.h"
#include "AzRecord.h"
#include "AzXmlFileDataSource.h"


//void TestFields::initTestCase() {

//}


//void TestFields::cleanupTestCase() {

//}


void TestFields::addFields() {

    AzField f;
    f.setType(QVariant::Int);
    f.setDisplayName("Целое");
    mFields.append("Int",f);

    f.setType(QVariant::String);
    f.setDisplayName("строка");
    mFields.append("String",f);

    f.setType(QVariant::Int);
    f.setDisplayName("Выч. поле");
    f.setFieldKind(AzField::CalcFieldKind);
    mFields.append("recState",f);

    QCOMPARE(mFields.size(),3);

    QFile file("Fields.xml");
    file.open(QIODevice::WriteOnly);

    mFields.write(&file);

}

void TestFields::clear() {
    QFile file("FieldsEmpty.xml");
    file.open(QIODevice::WriteOnly);

    mFields.clear();
    QCOMPARE(mFields.size(),0);

    mFields.write(&file);

}

void TestFields::read() {
    QFile file("Fields.xml");
    file.open(QIODevice::ReadOnly);

    try {
        mFields.read(&file);
    }

    catch (const AzException& e) {
        qDebug() << e.message();
    }
    QCOMPARE(mFields.size(),3);
}

void TestFields::saveRead() {
    QFile file("FieldsWriteReaded.xml");
    file.open(QIODevice::ReadWrite | QIODevice::Truncate);


    mFields.write(&file);

    //compare

    QFile file2("Fields.xml");
    file2.open(QIODevice::ReadOnly);

    QByteArray ba1 = file2.readAll();
    file.reset();
    QByteArray ba2 = file.readAll();
    QCOMPARE(ba1,ba2);
}
